// pages/member/index/index.js
import api from "../../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
      },
      {
        name: "carry_duration_form",
        type: "picker",
        label: "卡类型",
      },
    ],

    searchNum: [
      {
        label: "月卡",
        show: true,
        num: 0,
        subhead: "0节",
      },
      {
        label: "季卡",
        num: 0,
        show: true,
        subhead: "0节",
      },
      {
        label: "年卡",
        num: 0,
        show: true,
        subhead: "0节",
      },
    ],

    sExplain: {
      title: "搜索说明",
      content:
        "时间可以选择，对会员名称、手机号、会员类型、分配教练、分配状态、指导教练、指导状态进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对到店会员总人数，开门次数进行统计",
    },

    searchParam: {
      time_form: "today",
    },
    //#endregion

    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion

    //#region 教练
    coachShow: false,
    userID: null,
    userIndex: null,
    coachObj: {
      name: "coach_id",
    },
    //#endregion
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },

  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreCarryList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page === 1) {
          searchNum[0].num = data.entirety.month_earnings;
          searchNum[0].subhead = data.entirety.month_num + "节";
          searchNum[1].num = data.entirety.season_earnings;
          searchNum[1].subhead = data.entirety.season_num + "节";
          searchNum[2].num = data.entirety.year_earnings;
          searchNum[2].subhead = data.entirety.year_num + "节";
          this.setData({
            searchNum,
            "searchParam.shop_id": data.shop_info.shop_id,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map((i) => {
            i.user_info.tagStr = i.user_info.depict;
            i.user_info.tagVal = 0;
            if (i.guide_form === "have") i.user_info.guide_str = i.guide_str;

            return {
              ...i,
            };
          });
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
